package tasks 
{
	import app.AppModel;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import utils.ErrorsCollector;
	import utils.Logger;
	
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class BaseXmlLoadTask extends BaseTask
	{
		protected var _loader:URLLoader;
		protected var _xmlPath:String;
		protected var _taskRunning:Boolean = false;
		
		public function BaseXmlLoadTask(xmlPath:String) 
		{
			_xmlPath = xmlPath;
		}
		
		override public function run():void 
		{
			// run only once
			if (!_taskRunning)
			{
				_taskRunning = true;
				loadXml(_xmlPath);
				super.run();
			}
		}
		
		
		// загружает данные из xml
		protected function loadXml(xmlPath:String):void 
		{
			_loader = new URLLoader();
			addLoaderListeners(_loader);
			try
			{
				_loader.load(new URLRequest(xmlPath));
			}
			catch(er:Error)
			{
				handleError("IO Error on load XML. ID: "  + er.errorID + 
							" Name: " + er.name + " Message: " + er.message);
				complete(false);
				return;
			}
		}
		
		
		protected function readXml(e:Event):void 
		{
			removeLoaderListeners(_loader);
			var xml:XML;
			// читаем XML файл
			try
			{
				xml = new XML(e.target.data);
			}
			catch(err:Error)
			{
				handleError("Error while read XML: " + err.errorID + "; " + err.name + "; "  + err.message);
				complete(false);
				return;
			}
			
			onXMLReaded(xml);
		}
		
		protected function onXMLReaded(xml:XML):void 
		{
			
		}
		
		protected function addLoaderListeners(loader:URLLoader):void
		{
			if (loader == null)
			{
				return;
			}
			
			_loader.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
			_loader.addEventListener(Event.COMPLETE, readXml);
		}
		
		protected function removeLoaderListeners(loader:URLLoader):void
		{
			if (loader == null)
			{
				return;
			}
			loader.removeEventListener(IOErrorEvent.IO_ERROR, handleIOError);
			loader.removeEventListener(Event.COMPLETE, readXml);
		}
		
		protected function handleError(err:String):void
		{
			ErrorsCollector.collectError(this + ": " + _xmlPath + " " + err);
		}
		
		private function handleIOError(e:IOErrorEvent):void 
		{
			handleError("IO Error on load XML: " + e.text);
		}
		
		override public function destroy():void 
		{
			_xmlPath = "";
			removeLoaderListeners(_loader);
			_loader = null;
			super.destroy();
		}
		
		protected function getNumberFromStr(numStr:String, defaultNum:Number, errorReport:String, 
											defaultWithZeroLength:Boolean = false):Number
		{
			if (numStr.length == 0)
			{
				if (defaultWithZeroLength)
				{
					return defaultNum;
				}else
				{
					handleError(errorReport);
					return(defaultNum); 
				}
			}
			
			var num:Number = Number(numStr);
			if (isNaN(num))
			{
				handleError(errorReport);
				num = defaultNum;
			}
			return num;
		}
		
		protected function log(msg:String):void
		{
			Logger.log(this, msg);
		}
		
	}

}